from PyQt5 import QtWidgets
from cmdUI import Ui_CMDWindow
import sys
import sqlite3
import time

class Cmd(QtWidgets.QWidget,Ui_CMDWindow):
    def __init__(self,username='test',dbpath='test.db'):
        super(Cmd,self).__init__()
        self.setupUi(self)

        self.username=username
        self.dbpath=dbpath

        self.cmd_lineEdit.returnPressed.connect(self.CMD)

    def CMD(self):
        command = self.cmd_lineEdit.text()
        #init cmd_lienEdit
        self.cmd_lineEdit.setText("")
        
        conn =sqlite3.connect(self.dbpath)
        c = conn.cursor()
        help_command = ['h','H','help','HELP']
        
        if command =="clear":
            self.Output_textBrowser.clear()    
        elif command in help_command:
            info ='''\
command Example:
help command: h,H,help,HELP
cmdwinclear: clear the out info
sql cmd: INSERT,SELECT,UPDATE,DELETE...
tables(users,record,yeb)
users(date,name,salary,recordtime)
-----------------------------------------------\
'''
            self.Output_textBrowser.append(info)
        else:
            try:
                c.execute(command)
                outinfo = c.fetchall()
                info = str(outinfo)
            except sqlite3.OperationalError or sqlite3.IntegrityError :
                info ='SQL Error'
            rctime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            info = self.username + '>'+ command +'\n'+ info + '\n'+ rctime
            self.Output_textBrowser.append(info)

        c.close()
        conn.close()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    cmdwindow = Cmd(username='test',dbpath='test.db')
    cmdwindow.show()
    sys.exit(app.exec())

    
    
